#pragma once

#include <QObject>
#include <QScopedPointer>
#include <QSharedPointer>
#include <QProcess>
#include <QUuid>

struct TaskData
{
    bool silence;
    int type;
    QString uuid;
    QString name;
    QString exec;
    QStringList args;
    bool wait_on_success;
    bool stop_on_error;
    QSharedPointer<TaskData> success_task;
    QSharedPointer<TaskData> error_task;
    // TaskData *success_task;
    // TaskData *error_task;

    inline TaskData() : success_task(nullptr), error_task(nullptr) { reset(); }
    inline TaskData(int type) : success_task(nullptr), error_task(nullptr) { reset(); this->type = type; }
    inline void reset() {
        silence = false;
        type = -1;
        uuid.clear();
        name.clear();
        exec.clear();
        args.clear();
        wait_on_success = true;
        stop_on_error = true;
        // if (success_task) { delete success_task; success_task = nullptr; }
        // if (error_task) { delete error_task; error_task = nullptr; }
        success_task.clear();
        error_task.clear();
    }
};
Q_DECLARE_METATYPE(TaskData);

class TaskPrivate;
class Task : public QProcess
{
    Q_OBJECT
public:
    explicit Task(const TaskData &task, QObject *parent = nullptr);
    ~Task();

    const TaskData &data();
    const QString &name();
    const QString &uuid();

    bool is_running();

public Q_SLOTS:
    void Start();
    void Stop(bool force = false);

private:
    const QScopedPointer<TaskPrivate> d_ptr;
    Q_DECLARE_PRIVATE(Task)
};
